package org.jeecg.modules.icms.invoice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Sheet;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.icms.invoice.dto.IcmsInvoiceDetailDto;
import org.jeecg.modules.icms.invoice.entity.IcmsInvoiceHeader;
import org.jeecg.modules.icms.invoice.entity.IcmsInvoiceTitle;
import org.jeecg.modules.icms.invoice.mapper.IcmsInvoiceHeaderMapper;
import org.jeecg.modules.icms.invoice.mapper.IcmsInvoiceTitleMapper;
import org.jeecg.modules.icms.invoice.service.IIcmsInvoiceHeaderService;
import org.jeecg.modules.icms.invoice.vo.IcmsInvoiceHeaderVo;
import org.jeecg.modules.icms.util.HandleExcelInfo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 发票头表
 * @Author: jeecg-boot
 * @Date:   2024-01-10
 * @Version: V1.0
 */
@Service
public class IcmsInvoiceHeaderServiceImpl extends ServiceImpl<IcmsInvoiceHeaderMapper, IcmsInvoiceHeader> implements IIcmsInvoiceHeaderService {

    @Resource
    private IcmsInvoiceHeaderMapper icmsInvoiceHeaderMapper;

    @Resource
    private HandleExcelInfo handleExcelInfo;

    @Resource
    private IcmsInvoiceTitleMapper icmsInvoiceTitleMapper;

    @Override
    public IPage<IcmsInvoiceHeaderVo> queryAll(IcmsInvoiceHeaderVo icmsInvoiceHeaderVo, Integer pageNo, Integer pageSize, HttpServletRequest req){
        Page<IcmsInvoiceHeaderVo> page = new Page<IcmsInvoiceHeaderVo>(pageNo, pageSize);
        IPage<IcmsInvoiceHeaderVo> pagelist = icmsInvoiceHeaderMapper.queryAll(icmsInvoiceHeaderVo,page);
        return pagelist;
    }

    @Override
    public List<IcmsInvoiceHeader> queryListWithoutMatch() {
        QueryWrapper<IcmsInvoiceHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(IcmsInvoiceHeader::getMatchFlag, "0");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<IcmsInvoiceHeader> addHeaderInfo(Sheet sheet, List<IcmsInvoiceDetailDto> icmsInvoiceDetailDto, List<Integer> flagRows, IcmsInvoiceTitle icmsInvoiceTitle) throws Exception{
        List<IcmsInvoiceHeader> headerList = new ArrayList<IcmsInvoiceHeader>() ;
        //查询系统中已存在的合同号 避免重复
        List<String> contractNoList = icmsInvoiceHeaderMapper.findContractNo();
        //第一个开票头
        IcmsInvoiceHeader icmsInvoiceHeaderOne = new IcmsInvoiceHeader();
        //处理第一个Header 第一个Header固定在第9行
        icmsInvoiceHeaderOne.setPackageNo(sheet.getRow(8).getCell(0).toString());

        //第一个Header中 合同号不能重复
        String contractNo = sheet.getRow(8).getCell(1).toString();
        if(contractNoList.contains(contractNo)){
            throw new JeecgBootException(sheet.getSheetName()+"表中合同号："+contractNo+"在系统中已存在，禁止导入！");
        }
        //同一个表中的其他合同号也不能重复
        contractNoList.add(contractNo);
        icmsInvoiceHeaderOne.setContractNo(contractNo);
        icmsInvoiceHeaderOne.setExportCountry(sheet.getRow(8).getCell(4).toString());
        icmsInvoiceHeaderOne.setExportNo(sheet.getRow(8).getCell(5).toString());
        icmsInvoiceHeaderOne.setDealUsername(sheet.getRow(8).getCell(6).toString());
        icmsInvoiceHeaderOne.setInvoiceTitleId(icmsInvoiceTitle.getId());

        //数据为空判定 待定
        if(handleExcelInfo.checkObjAllFieldsIsNull(icmsInvoiceHeaderOne))
        {
//            headerList.add(icmsInvoiceHeaderOne);
//            return headerList;
            throw new JeecgBootException(sheet.getSheetName()+"表中无开票头信息或格式错误！");
        }
        headerList.add(icmsInvoiceHeaderOne);

        //处理其他header
        for(Integer invoiceRemarkRow : flagRows){
            IcmsInvoiceHeader icmsInvoiceHeader = new IcmsInvoiceHeader();
            icmsInvoiceHeader.setPackageNo(icmsInvoiceDetailDto.get(invoiceRemarkRow-1).getPartName());

            //合同重复 不再导入
            String contractNos = icmsInvoiceDetailDto.get(invoiceRemarkRow-1).getPartNo();
            if(contractNoList.contains(contractNos))
            {
                throw new JeecgBootException(sheet.getSheetName()+"表中合同号："+contractNos+"在系统中已存在，禁止导入！");
            }

            //一个表里的合同号也不能重复
            icmsInvoiceHeader.setContractNo(contractNos);
            contractNoList.add(contractNos);

            icmsInvoiceHeader.setExportCountry(icmsInvoiceDetailDto.get(invoiceRemarkRow-1).getPartNameEn());
            icmsInvoiceHeader.setExportNo(icmsInvoiceDetailDto.get(invoiceRemarkRow-1).getAccountNature());
            icmsInvoiceHeader.setDealUsername(icmsInvoiceDetailDto.get(invoiceRemarkRow-1).getRemark());
            icmsInvoiceHeader.setInvoiceTitleId(icmsInvoiceTitle.getId());
            headerList.add(icmsInvoiceHeader);
        }
        return headerList;
    }
}
